package com.example.fengyungo.service.impl;

import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.dao.DriverDao;
import com.example.fengyungo.domain.pojo.Driver;
import com.example.fengyungo.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverDao driverDao;

    /**
     * 通过用户名和密码查找司机
     *
     * @param username 用户名
     * @param password 密码
     * @return drivers List<Driver>
     */
    @Override
    public List<Driver> getDriverByUsernameAndPassword(String username, String password) {
        List<Driver> drivers = driverDao.getDriverByUsernameAndPassword(username, password);
        if (drivers.size() > 0) {
            // 隐藏密码
            drivers.get(0).setPassword("");
        }
        return drivers;
    }

    /**
     * 新增司机
     *
     * @param driver Driver
     * @return Boolean
     */
    @Override
    public Boolean addDriver(Driver driver) {
        // 填充driver的空值
        // 根据注册时间生成id
        DateTimeFormatter formatterForId = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        DateTimeFormatter formatterForCreateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        // TODO: 2024/1/22 id可能重复
        String id = now.format(formatterForId);
        String createTime = now.format(formatterForCreateTime);
        driver.setId("d" + id);
        driver.setCreateTime(createTime);
        driver.setStatus(DriverConstant.STATUS_NOT_ACCESS);
        driver.setSex(DriverConstant.UNSETTED);
        driver.setPhone(DriverConstant.UNSETTED);
        driver.setIdCard(DriverConstant.UNSETTED);
        driver.setAvatar(DriverConstant.DEFAULT_AVATAR);
        driver.setNickname(DriverConstant.UNSETTED);
        Boolean res = driverDao.addDriver(driver);

        return res;
    }

    /**
     * 通过用户名（账号）或邮箱查找司机
     *
     * @param username 用户名，账号
     * @return List<Driver>
     */
    @Override
    public Driver getDriverByUsername(String username) {
        Driver driver = driverDao.getDriverByUsername(username);
        return driver;
    }

    /**
     * 通过邮箱查找司机
     *
     * @param mail 邮箱
     * @return List<Driver>
     */
    @Override
    public List<Driver> getDriverByMail(String mail) {
        List<Driver> drivers = driverDao.getDriverByMail(mail);
        return drivers;
    }

    /**
     * 更新司机昵称
     *
     * @param nickname 昵称
     * @param id       司机id
     * @return Integer
     */
    @Override
    public Integer updateNicknameById(String nickname, String id) {
        Integer res = driverDao.updateNicknameById(nickname, id);
        return res;
    }

    /**
     * 更新司机性别
     *
     * @param sex 性别
     * @param id  司机id
     * @return Integer
     */
    @Override
    public Integer updateSexById(String sex, String id) {
        Integer res = driverDao.updateSexById(sex, id);
        return res;
    }

    /**
     * 判断司机是否具有接单权利
     *
     * @param id 司机id
     * @return Boolean是否具有
     */
    @Override
    public Boolean judgeStatus(String id) {
        Integer statusById = driverDao.getStatusById(id);
        // 司机是否已经认证为司机
        return statusById == DriverConstant.VERIFIED;
    }

    /**
     * 更新司机电话号码
     *
     * @param phone 电话号码
     * @param id    司机id
     * @return Integer
     */
    @Override
    public Integer updatePhoneById(String phone, String id) {
        Integer res = driverDao.updatePhoneById(phone, id);
        return res;
    }

    /**
     * 修改司机信息
     *
     * @param driver 司机
     * @return 更新结果数
     */
    @Override
    public Integer updateDriverById(Driver driver) {
        Integer res = driverDao.updateDriverById(driver);
        return res;
    }

    /**
     * 修改司机状态
     *
     * @param driverId 司机id
     * @return Integer
     */
    @Override
    public Integer updateDriverStatusById(String driverId, int status) {
        Driver driver = new Driver();
        driver.setId(driverId);
        driver.setStatus(status);
        Integer res = driverDao.updateDriverById(driver);
        return res;
    }

    /**
     * 通过id获取司机电话
     *
     * @param id 司机id
     * @return String
     */
    @Override
    public String getPhoneById(String id) {
        return driverDao.getPhoneById(id);
    }

    /**
     * 查询愿意接收订单的司机
     *
     * @param number 订单编号
     * @return List<Driver>
     */
    @Override
    public List<Driver> getDriversByOrderNumber(String number) {
        return driverDao.getDriversByOrderNumber(number);
    }

    /**
     * 获取司机旧密码
     *
     * @param username 用户名
     * @return String
     */
    @Override
    public String getOldPassword(String username) {
        return driverDao.getPasswordByUsername(username);
    }

    /**
     * 修改司机密码
     *
     * @param username 用户名
     * @param password 新密码
     * @return Integer
     */
    @Override
    public Integer updatePassword(String username, String password) {
        return driverDao.updatePassword(username, password);
    }

    /**
     * 修改司机评分为新的平均分
     *
     * @param id 司机id
     * @return Integer
     */
    @Override
    public Integer updateCredit(String id) {
        return driverDao.updateCredit(id);
    }

    /**
     * 通过id获取司机信息
     *
     * @param id 司机id
     * @return Driver
     */
    @Override
    public Driver getDriverById(String id) {
        return driverDao.getDriverById(id);
    }

    /**
     * 条件查询司机
     *
     * @param driver Driver
     * @return List<Driver>
     */
    @Override
    public List<Driver> getDriverByCondition(Driver driver) {
        return driverDao.getDriverByCondition(driver);
    }

    /**
     * 查询所有司机
     *
     * @return List<Driver>
     */
    @Override
    public List<Driver> getAllDrivers() {
        List<Driver> allDrivers = driverDao.getAllDrivers();
        return allDrivers;
    }
}
